﻿/*  Copyright © 2012 Matthew David Elgert - mdelgert@yahoo.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

using System;
using System.Collections;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SevenZip;

namespace BLL
{
    /// <summary>
    /// Based on: http://sevenzipsharp.codeplex.com/
    /// </summary>

    public static class SevenZipUtil
    {

        public static void CompressFiles()
        {

            using (DataTable dt = Common.GetDBNames())
            {
                foreach (DataRow row in dt.Rows)
                {
                    CompressFile(row[0].ToString());
                }
            }

        }

        public static void CompressFile(string DBName)
        {

            string TempPath = Common.GetTempDir() + DBName + Common.GetTimeStamp();

            Log.Msg("Compressing " + DBName + " begin");
            Common.ShowProgress(true);

            try
            {

                var tmp = new SevenZipCompressor();
                tmp.ScanOnlyWritable = true;

                if (Common.GetArchivePwd() != null)
                {
                    tmp.ZipEncryptionMethod = ZipEncryptionMethod.Aes256;
                    tmp.CompressDirectory(TempPath, TempPath + ".7z", Common.GetArchivePwd());
                }
                else
                {
                    tmp.CompressDirectory(TempPath, TempPath + ".7z");
                }

                Common.ShowProgress(false);

                Log.Msg("Compressing " + DBName + " end");

                if (Common.GetDelBakTemp() == true)
                {
                    Common.DeleteFileTemp(DBName);
                }

            }
            catch (System.Exception ex)
            {
                Log.Msg("Compressing " + DBName + " failed", true);
                Log.Msg(ex.ToString(), true);
            }

        }

    }

}

